<?php
/**
 * Object Relational Persistence Mapping Class for table: package_item
 *
 * @package	com.athena
 * @author	LHV
 */
class PackageItem extends ORDataObject {

	/**#@+
	 * Fields of table: package_item mapped to class members
	 */
	var $package_item_id		= '';
	var $package_id		= '';
	var $item_order		= '';
	var $room_id		= '';
	var $item_type		= '';
	/**#@-*/

	const SERVICE_TYPE = "service";
	const TEST_TYPE = "test";
	
	/**
	 * DB Table
	 */
	var $_table = 'package_item';

	/**
	 * Primary Key
	 */
	var $_key = 'package_item_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'PackageItem';

	/**
	 * Handle instantiation
	 */
	function PackageItem() {
		parent::ORDataObject();
	}


	
	public function getAllPackageItem($packageId) {
		$ds = new Datasource_sql();
		$sql = array(
			'cols' 		=> "package_item_id, item_type",
			'from' 		=> "package_item",
			'where' 	=> "package_id=$packageId", 			
			'orderby' 	=> "item_order"
			);
					
		$ds->setup($this->_db,$sql);
		return $ds->toArray('package_item_id', 'item_type');
	}
	
	public function addTest($packageId, $testId, $testItem) {
		$test = Celini::newORDO('Test', $testId);
		$roomId = $test->get('room_id');
		
		$itemOrder = $this->getItemOrder($packageId);		
		$data = array (				
				'package_id'		=> $packageId,	
				'room_id'		=> $roomId,											
				'item_order'	=> $itemOrder,								
				'item_type'			=> self::TEST_TYPE				
			);
		$this->populateArray($data);
		$this->persist();
		
		$packageItemId = $this->get_id();
		$packageTestItemOrdo = Celini::newORDO('PackageTestItem');
		$packageTestItemOrdo->addItem($packageItemId, $testId, $testItem);
	}
	
	public function addService($packageId, $servRoomId) {
		$serRoom = Celini::newORDO('ServiceRoom', $servRoomId);
		$roomId = $serRoom->get('room_id');
		
		$packageOrder = $this->getItemOrder($packageId);		
		$data = array (				
				'package_id'		=> $packageId,	
				'room_id'		=> $roomId,											
				'item_order'	=> $packageOrder,								
				'item_type'			=> self::SERVICE_TYPE				
			);
		$this->populateArray($data);
		$this->persist();
		
		$packageItemId = $this->get_id();		
		$servItem = Celini::newORDO('PackageServiceItem');
		$servItem->addItem($packageItemId, $servRoomId);
	}
	
	public function getServiceItem($packageItemId) {	
	    $field        =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');					
		$ds = new Datasource_sql();
		$sql = array (
			'cols'		=> "S.$field service_name, S.price,
						S.payment_type, R.$field room_name,
						PI.package_item_id, PI.item_order, PI.item_type",						
			'from'		=> "package_item PI 
				INNER JOIN package_service_item SI on PI.package_item_id = SI.package_item_id
				INNER JOIN service_room SR on SI.service_room_id = SR.service_room_id
				INNER JOIN service S on S.service_id = SR.service_id
				INNER JOIN room R on R.room_id = SR.room_id",
			'where'		=> "PI.package_item_id = $packageItemId",			
		);				
			
		$ds->setup($this->_db, $sql);
		$res = $ds->toArray();
		if (sizeof($res) > 0) {
			return $res[0];									
		}
		return false;
	}
	
	public function getTestItem($packageItemId) {
	    $field        =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		
		$ds = new Datasource_sql();
		$sql = array (
			'cols'		=> "TE.$field service_name, TE.payment_type, 
						R.$field room_name,
						PI.package_item_id, PI.item_order, PI.item_type",						
			'from'		=> "package_item PI 
				INNER JOIN package_test_item TE_I on PI.package_item_id = TE_I.package_item_id
				INNER JOIN test TE on TE_I.test_id = TE.test_id				
				INNER JOIN room R on R.room_id = TE.room_id",
			'where'		=> "PI.package_item_id = $packageItemId",			
			);				
			
		$ds->setup($this->_db, $sql);
		$res = $ds->toArray();	
		
		if (sizeof($res) > 0) {	
			$res[0]['price'] = $this->getTestPrice($packageItemId);				
			return $res[0];
		}
		return false;
	}		
	
	public function deleteItem($packageItemId) {
		$this->setup($packageItemId);
		$type = $this->get('item_type');
		
		if ($type == self::SERVICE_TYPE) {
			// delete service item if available
			$serviceItem = Celini::newORDO('PackageServiceItem');
			$serviceItem->delete($packageItemId);
		} else if ($type == self::TEST_TYPE) {
			// delete test item if available
			$testItem = Celini::newORDO("PackageTestItem");
			$testItem->delete($packageItemId);
		}
		$order = $this->get('item_order');	
		$packageId = $this->get('package_id');	
		$sql = "update package_item set item_order = item_order - 1  
			 where package_id = $packageId  AND item_order > $order";
		$this->_execute($sql);
		
		// delete the package_item item
		$sql = "delete from package_item where package_item_id = $packageItemId";
		$this->_execute($sql);
	}
	
	private function getItemOrder($packageId) {		
		$sql = "select max(item_order) num_item from $this->_table where package_id = $packageId";
		$res = $this->_execute($sql);			
		if ($res && !$res->EOF) {
			return $res->fields['num_item'] + 1;
		}				
		return 1;
	}
	
	private function getTestPrice($packageItemId) {
		$ti = Celini::newORDO('PackageTestAssoService');
		$res = $ti->getServiceInATest($packageItemId);
		
		$totalPrice = 0;		
		foreach ($res as $s) {
			$serv = Celini::newORDO('Service', $s['service_id']);
			$totalPrice += $serv->get('price');
		}
		return $totalPrice;
	}
	
}
?>
